package com.sanqing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sanqing.dao.base.BaseDao;
import com.sanqing.po.Order;
import com.sanqing.service.OrderService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;
import com.sanqing.util.PageUtil;

@Service("orderService")
@Transactional(readOnly = true)
public class OrderServiceImpl implements OrderService {

	@Resource(name = "orderDao")
	private BaseDao<Order, String> orderDao;

	@Override
	public PageResult queryOrderByPage(Page page, Order order) {
		Map<String, Object> map = new HashMap<String, Object>();
		String hql = "from Order a where 1=1";

		page = PageUtil.createPage(page.getEveryPage(), (int) orderDao.countHqlResult(hql, map), page.getCurrentPage());// 根据总记录数创建分页信息
		List<Order> list = orderDao.findByPage(page, hql, map);// 通过分页信息取得试题
		PageResult result = new PageResult(page, list);// 封装分页信息和记录信息，返回给调用处
		return result;
	}

	@Override
	@Transactional(readOnly = false)
	public void add(Order order) {
		orderDao.save(order);
	}

	@Override
	public Order getById(String id) {
		return orderDao.getByKey(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void edit(Order order) {
		Order r = orderDao.getByKey(order.getId());
		BeanUtils.copyProperties(order, r);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		orderDao.delete(id);
	}

}
